<?php

namespace app\common\controller;

use app\BaseController;


class Base extends BaseController
{

    // 初始化方法
    protected function initialize()
    {
        // 调用父类初始化方法
        parent::initialize();

        // 添加其他初始化操作
    }

    protected function returnJsonData(int $code, array|null $data = [], string $msg = '')
    {
        if (!is_numeric($code)) {
            throw new \think\Exception('The code must be a numeric value.');
        }

        $codeMessage = config()['code'] ?? 500001;
        if (!isset($codeMessage[$code])) {
            // throw new \think\Exception('The code does not have an associated message.');
            $codeMessage[$code] = $msg;
        }

        $result = [
            'code' => $code,
            'message' => $codeMessage[$code] . ',' . $msg,
            'data' => $data
        ];
        return json($result);
    }
}
